//
//  VipCardFormatDayView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/31.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipCardFormatDayView.h"
#import "MOFSPickerManager.h"

@implementation VipCardFormatDayView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSClearColor;
                        
        
        /**
         订单号
         */
        CommonInputView *orderNumInputView = [[CommonInputView alloc] init];
        orderNumInputView.titleLabel.text = @"订单号";
        [self addSubview:orderNumInputView];
        self.orderNumInputView = orderNumInputView;
        
        
        /**
         选择会员
         */
        CommonBtnView *selectVipBtnView = [[CommonBtnView alloc] init];
        selectVipBtnView.titleLabel.text = @"选择会员*";
        selectVipBtnView.choseLabel.text = @"请选择";
        [self addSubview:selectVipBtnView];
        self.selectVipBtnView = selectVipBtnView;
        
        /**
         充值次数
         */
        CommonInputView *chargeTimeInputView = [[CommonInputView alloc] init];
        chargeTimeInputView.titleLabel.text = @"充值次数*";
        [self addSubview:chargeTimeInputView];
        //chargeTimeInputView.hidden = YES;
        self.chargeTimeInputView = chargeTimeInputView;
        
        /**
         设置有效期
         */
        CommentSwitchView *setValidSwitchView = [[CommentSwitchView alloc] init];
        setValidSwitchView.titleLabel.text = @"设置有效期";
        [setValidSwitchView.titleSwitch setOn:YES];
        [setValidSwitchView.titleSwitch addTarget:self action:@selector(setValidSwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        //setValidSwitchView.hidden = YES;
        [self addSubview:setValidSwitchView];
        self.setValidSwitchView = setValidSwitchView;
        
        /**
         开始日期
         */
        CommonBtnView *beginTimeBtnView = [[CommonBtnView alloc] init];
        beginTimeBtnView.titleLabel.text = @"开始日期*";
        beginTimeBtnView.choseLabel.text = @"选择日期";
        beginTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        beginTimeBtnView.choseBtn.tag = 0;
        [beginTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        [self addSubview:beginTimeBtnView];
        self.beginTimeBtnView = beginTimeBtnView;
        
        /**
         结束日期
         */
        CommonBtnView *endTimeBtnView = [[CommonBtnView alloc] init];
        endTimeBtnView.choseBtn.tag = 1;
        [endTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        endTimeBtnView.titleLabel.text = @"结束日期*";
        endTimeBtnView.choseLabel.text = @"选择日期";
        endTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        [self addSubview:endTimeBtnView];
        self.endTimeBtnView = endTimeBtnView;
        
        /**
         自动开卡
         */
        CommentSwitchView *outoSwitchView = [[CommentSwitchView alloc] init];
        outoSwitchView.titleLabel.text = @"自动开卡";
        [self addSubview:outoSwitchView];
        self.outoSwitchView = outoSwitchView;
        
        /**
         详情Label
         */
        TTTAttributedLabel *detailLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        detailLabel.font = SmallFont;
        detailLabel.textColor = CSColorFromRGB(0x7a7a7a);
        detailLabel.numberOfLines = 0;
        detailLabel.lineBreakMode = NSLineBreakByCharWrapping;
        detailLabel.text = @"开启【自动开卡】功能后，会员可以在开始日期之前使用该会员卡，一旦提前使用，开始日期自动从使用当天开始计算";
        [self addSubview:detailLabel];
        self.detailLabel = detailLabel;
        
        /**
         应付金额
         */
        CommonInputView *copeWithInputView = [[CommonInputView alloc] init];
        copeWithInputView.titleLabel.text = @"应付金额*";
        [self addSubview:copeWithInputView];
        self.copeWithInputView = copeWithInputView;
        
        /**
         实收金额
         */
        CommentSwitchView *receiptSwitchView = [[CommentSwitchView alloc] init];
        receiptSwitchView.titleLabel.text = @"实收金额";
        [receiptSwitchView.titleSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        [receiptSwitchView.titleSwitch setOn:YES];
        [self addSubview:receiptSwitchView];
        self.receiptSwitchView = receiptSwitchView;

        TTTAttributedLabel *recepitNoticeLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        recepitNoticeLabel.font = SmallFont;
        recepitNoticeLabel.textColor = CSColorFromRGB(0x7a7a7a);
        recepitNoticeLabel.numberOfLines = 0;
        recepitNoticeLabel.lineBreakMode = NSLineBreakByCharWrapping;
        recepitNoticeLabel.text = @"开启【实收金额】功能后，可以手动输入实际收到的金额";
        [self addSubview:recepitNoticeLabel];
        self.recepitNoticeLabel = recepitNoticeLabel;

        CommonInputView *receiptInputView = [[CommonInputView alloc] init];
        receiptInputView.titleLabel.text = @"实收金额(元)*";
        [self addSubview:receiptInputView];
        self.receiptInputView = receiptInputView;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    /**
     订单号
     */
    [self.orderNumInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self).offset(10);
        
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     充值次数
     */
         
    [self.chargeTimeInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.orderNumInputView.mas_bottom).offset(10);
        
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     选择会员
     */
    [self.selectVipBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        if (self.chargeTimeInputView) {
        
            make.top.mas_equalTo(self.chargeTimeInputView.mas_bottom).offset(10);
        }
        else{
            
            make.top.mas_equalTo(self.orderNumInputView.mas_bottom).offset(10);
        }
        
        make.left.width.mas_equalTo(self);
    }];
    
     
    /**
     设置有效期
     */
    [self.setValidSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.selectVipBtnView.mas_bottom).offset(10);
        
        make.left.width.mas_equalTo(self);
    }];
    
    if (self.beginTimeBtnView) {
     
        /**
         开始日期
         */
        [self.beginTimeBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            if (self.setValidSwitchView) {
                
                make.top.mas_equalTo(self.setValidSwitchView.mas_bottom);
            }
            else{
                
                make.top.mas_equalTo(self.selectVipBtnView.mas_bottom).offset(10);
            }
            
            make.left.width.mas_equalTo(self);
        }];
    }
    
    if (self.endTimeBtnView) {
     
        /**
         结束日期
         */
        [self.endTimeBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.beginTimeBtnView.mas_bottom);
            
            make.left.width.mas_equalTo(self);
        }];
    }
    
    /**
     自动开卡
     */
    [self.outoSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        if (self.setValidSwitchView ) {
            
            if (self.setValidSwitchView.titleSwitch.isOn) {
            
                make.top.mas_equalTo(self.endTimeBtnView.mas_bottom).offset(10);
            }
            else{
            
                make.top.mas_equalTo(self.setValidSwitchView.mas_bottom).offset(10);
            }
        }
        else{
            
            //如果是期限卡 期限卡也没有设置有效期
            if ([self.type intValue] == Term) {
                
                //如果是有期限的
                if (self.valid) {
                    
                    make.top.mas_equalTo(self.beginTimeBtnView.mas_bottom).offset(10);
                }
                else{
                    if (self.endTimeBtnView) {
                    
                        make.top.mas_equalTo(self.endTimeBtnView.mas_bottom).offset(10);
                    }
                    else{
                        make.top.mas_equalTo(self.selectVipBtnView.mas_bottom).offset(10);
                    }
                }
            }
            else{
             
                //如果是有期限的
                if (self.valid) {
                    
                    make.top.mas_equalTo(self.beginTimeBtnView.mas_bottom).offset(10);
                }
                else{
                    
                    make.top.mas_equalTo(self.selectVipBtnView.mas_bottom).offset(10);
                }
            }
            
        }        
        
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     详情Label
     */
    [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.outoSwitchView.mas_bottom).offset(10);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.right.mas_equalTo(self).offset(-10);
    }];
    
    /**
     应付金额
     */
    [self.copeWithInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.detailLabel.mas_bottom).offset(10);
        
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     实收金额
     */
    [self.receiptSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(self.copeWithInputView.mas_bottom).offset(10);

        make.left.width.mas_equalTo(self);
        
    }];

    [self.recepitNoticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.receiptSwitchView.mas_bottom).offset(10);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.right.mas_equalTo(self).offset(-10);
        
        if (!self.receiptSwitchView.titleSwitch.isOn) {
            
            make.bottom.mas_equalTo(self).offset(-10);
        }
    }];
    
    if (self.receiptSwitchView.titleSwitch.isOn) {

        /**
         实收金额
         */
        [self.receiptInputView mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(self.recepitNoticeLabel.mas_bottom).offset(10);

            make.left.width.mas_equalTo(self);

            make.bottom.mas_equalTo(self).offset(-10);
        }];
    }
    
//    [self setNeedsLayout];
//    [self layoutIfNeeded];
    [self.superview.superview setNeedsLayout];
    [self.superview.superview layoutIfNeeded];
}


/**
 实收金额切换

 */
-(void)switchAction:(id)sender
{
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        CommonInputView *receiptInputView = [[CommonInputView alloc] init];
        receiptInputView.titleLabel.text = @"实收金额(元)*";
        [self addSubview:receiptInputView];
        self.receiptInputView = receiptInputView;
    }
    else{
        
        [self.receiptInputView removeFromSuperview];
        self.receiptInputView = nil;
    }
    
    [self setConstraints];
    
    [self.superview.superview setNeedsLayout];
    [self.superview.superview layoutIfNeeded];
}


/**
 设置有效期切换
 */
-(void)setValidSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        /**
         开始日期
         */
        CommonBtnView *beginTimeBtnView = [[CommonBtnView alloc] init];
        beginTimeBtnView.choseBtn.tag = 0;
        [beginTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        beginTimeBtnView.titleLabel.text = @"开始日期*";
        beginTimeBtnView.choseLabel.text = @"选择日期";
        beginTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        [self addSubview:beginTimeBtnView];
        self.beginTimeBtnView = beginTimeBtnView;
        
        /**
         结束日期
         */
        CommonBtnView *endTimeBtnView = [[CommonBtnView alloc] init];
        endTimeBtnView.choseBtn.tag = 1;
        [endTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        endTimeBtnView.titleLabel.text = @"结束日期*";
        endTimeBtnView.choseLabel.text = @"选择日期";
        endTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        [self addSubview:endTimeBtnView];
        self.endTimeBtnView = endTimeBtnView;

    }
    else{
        
        [self.beginTimeBtnView removeFromSuperview];
        [self.endTimeBtnView removeFromSuperview];
        self.beginTimeBtnView = nil;
        self.endTimeBtnView = nil;
        self.beginTimeStr = @"";
        self.endTimeStr = @"";
    }
    
    [self setConstraints];

    [self.superview.superview setNeedsLayout];
    [self.superview.superview layoutIfNeeded];
}

/**
 设置卡类型
 */
-(void)setType:(NSString *)type{
    
    _type = type;
    
    [self.chargeTimeInputView removeFromSuperview];
    [self.setValidSwitchView removeFromSuperview];
    [self.beginTimeBtnView removeFromSuperview];
    [self.endTimeBtnView removeFromSuperview];
    self.chargeTimeInputView = nil;
    self.setValidSwitchView = nil;
    self.beginTimeBtnView = nil;
    self.endTimeBtnView = nil;
    self.beginTimeStr = @"";
    self.endTimeStr = @"";
    /**
     充值次数
     */
    CommonInputView *chargeTimeInputView = [[CommonInputView alloc] init];
    chargeTimeInputView.titleLabel.text = @"充值次数*";
    [self addSubview:chargeTimeInputView];
    //chargeTimeInputView.hidden = YES;
    self.chargeTimeInputView = chargeTimeInputView;
    
    /**
     设置有效期
     */
    CommentSwitchView *setValidSwitchView = [[CommentSwitchView alloc] init];
    setValidSwitchView.titleLabel.text = @"设置有效期";
    [setValidSwitchView.titleSwitch setOn:YES];
    [setValidSwitchView.titleSwitch addTarget:self action:@selector(setValidSwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
    //setValidSwitchView.hidden = YES;
    [self addSubview:setValidSwitchView];
    self.setValidSwitchView = setValidSwitchView;
    
    /**
     开始日期
     */
    CommonBtnView *beginTimeBtnView = [[CommonBtnView alloc] init];
    beginTimeBtnView.choseBtn.tag = 0;
    [beginTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
    beginTimeBtnView.titleLabel.text = @"开始日期*";
    beginTimeBtnView.choseLabel.text = @"选择日期";
    beginTimeBtnView.choseLabel.textColor = PlaceHoldColor;
    [self addSubview:beginTimeBtnView];
    self.beginTimeBtnView = beginTimeBtnView;
    
    /**
     结束日期
     */
    CommonBtnView *endTimeBtnView = [[CommonBtnView alloc] init];
    endTimeBtnView.choseBtn.tag = 1;
    [endTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
    endTimeBtnView.titleLabel.text = @"结束日期*";
    endTimeBtnView.choseLabel.text = @"选择日期";
    endTimeBtnView.choseLabel.textColor = PlaceHoldColor;
    [self addSubview:endTimeBtnView];
    self.endTimeBtnView = endTimeBtnView;
    
    //如果类型不是等于次卡类型的话
    if ([type intValue] != Time) {
        
        /**
         充值次数
         */
        [self.chargeTimeInputView removeFromSuperview];
        self.chargeTimeInputView = nil;
    }
    
    //如果是期限卡
    if ([type intValue] == Term) {
        
        /**
         设置有效期
         */
        [self.setValidSwitchView removeFromSuperview];
        self.setValidSwitchView = nil;
    }
    
    [self setConstraints];
    
    [self.superview.superview setNeedsLayout];
    [self.superview.superview layoutIfNeeded];
}

/**
 规格的有效期
 */
-(void)setValid:(BOOL)valid{
    
    _valid = valid;
    
    [self.chargeTimeInputView removeFromSuperview];
    [self.setValidSwitchView removeFromSuperview];
    [self.beginTimeBtnView removeFromSuperview];
    [self.endTimeBtnView removeFromSuperview];
    self.chargeTimeInputView = nil;
    self.setValidSwitchView = nil;
    self.beginTimeBtnView = nil;
    self.endTimeBtnView = nil;
    //有设置有效期
    if (valid) {
        
        /**
         开始日期
         */
        CommonBtnView *beginTimeBtnView = [[CommonBtnView alloc] init];
        beginTimeBtnView.choseBtn.tag = 0;
        [beginTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        beginTimeBtnView.titleLabel.text = @"开始日期*";
        beginTimeBtnView.choseLabel.text = @"选择日期";
        beginTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        [self addSubview:beginTimeBtnView];
        self.beginTimeBtnView = beginTimeBtnView;
    }
    else{
        
        [self.beginTimeBtnView removeFromSuperview];
        self.beginTimeBtnView = nil;
        self.beginTimeStr = @"";
    }
    
    [self setConstraints];
    
    [self.superview.superview setNeedsLayout];
    [self.superview.superview layoutIfNeeded];
}

-(void)beginTimeBtnClick:(UIButton *)btn{
    
    [self endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:110 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
        
        dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
        
        if (btn.tag == 0) {
         
            weakSelf.beginTimeBtnView.choseLabel.text = [dateFormatter stringFromDate:date];
            
            weakSelf.beginTimeBtnView.choseLabel.textColor = TFColor;
            
            weakSelf.beginTimeStr = [dateFormatter stringFromDate:date];
        }
        else{
            
            weakSelf.endTimeBtnView.choseLabel.text = [dateFormatter stringFromDate:date];
            
            weakSelf.endTimeBtnView.choseLabel.textColor = TFColor;
            
            weakSelf.endTimeStr = [dateFormatter stringFromDate:date];
        }
        
    } cancelBlock:^{
        
        
    }];
}
@end
